package mindshare;

import android.content.Context;

public class Distance {
	private String currentLocation;
	double radioTierra;
	//double latFija;
	double latCelular;
	//double longFija;
	double longCelular;
	double difLat;
	double difLong;
	double a;
	double c;
	double distancia;
	Context context;
	
	
	public Distance(double latCelular,double longCelular,Context context){
		this.context=context;
		this.latCelular=latCelular;
		this.longCelular=longCelular;
		radioTierra = 6373; //radio terrestre medio, en metros
		
		//Cedetec
		double longServidor1 = -99.2275927;
		double latServidor1 = 19.59720188;
		
		//Aulas 6
		double longServidor2 = -99.22571226;
		double latServidor2 = 19.59885605;
		
		
//		Log.d("DEBUGEANDO","Distancia Cedetec:");
		double distancia1=calcularDistancia(latServidor1, longServidor1);
//		Log.d("DEBUGEANDO","Distancia Aulas:");
		double distancia2=calcularDistancia(latServidor2, longServidor2);
		
		
		if(distancia1<distancia2){
//			Log.d("DEBUGEANDO", "ESTÁ MÁS CERCA DEL SERVIDOR CEDETEC");
			setCurrentLocation("Cedetec");
//			Toast.makeText(context,"SERVIDOR CEDETEC", Toast.LENGTH_SHORT).show();
		}else{
//			Log.d("DEBUGEANDO", "ESTÀ MÁS CERCA DEL SERVIDOR AULAS");
			setCurrentLocation("Aulas");
//			Toast.makeText(context,"SERVIDOR AULAS", Toast.LENGTH_SHORT).show();
		}		
	}
	
	private double calcularDistancia(double latFija,double longFija){
		difLat=latCelular-latFija;
		difLong=longCelular-longFija;
		a = (Math.sin(difLat/2))*(Math.sin(difLat/2)) + Math.cos(latFija) * Math.cos(latCelular) * (Math.sin(difLong/2))*(Math.sin(difLong/2)); 
		c = 2 * Math.atan2( Math.sqrt(a), Math.sqrt(1-a)); 
		distancia = radioTierra * c;
//		Log.d("DEBUGEANDO",distancia+"");
		return distancia;
		
	}

	public String getCurrentLocation() {
		return currentLocation;
	}

	public void setCurrentLocation(String currentLocation) {
		this.currentLocation = currentLocation;
	}
}
